#include "myrunable.h"
#include <ctime>
#include <QThread>
#include <cstdlib>
#include <QDebug>
MyRunable::MyRunable():flag(false)
{

}



MyRunable::MyRunable(int startId, int count, redisContext *context):m_startId(startId),m_count(count),m_ctx(context)
{
    srand(time(0));
    setAutoDelete(true);
}

void MyRunable::stopSend()
{
    flag = true;
}

int MyRunable::generateValue()
{

    return ( rand() % (100 - 1 + 1) ) + 1 ;


}
void MyRunable::run()
{
    while (!flag) {
        // 批量写入当前线程负责的数据
        for (int i = 0; i < m_count; ++i) {
            int id = m_startId + i;
            int value = generateValue(); // 生成你的float值
            // 使用Redis的Pipeline批量发送命令
            redisAppendCommand(m_ctx, "SET analog:%d %d", id, value);
        }

        // 执行批量命令
        for (int i = 0; i < m_count; ++i) {
            redisReply* reply;
            redisGetReply(m_ctx, (void**)&reply);
            freeReplyObject(reply);
        }
        QThread::msleep(500);
    }


}
